package com.inossem.warehousestorage_inossem.adapter.inventory;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.inventory.InventoryExtra;
import com.inossem.warehousestorage_inossem.activity.inventory.InventoryOverViewActivity;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryMgrBean;

/**
 * Created by cjz on 2017/6/6.
 */

public class InventoryListAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private InventoryMgrBean.DataBean bean;

    public InventoryListAdapter(Context context,InventoryMgrBean.DataBean bean) {
        mContext = context;
        inflater = LayoutInflater.from(context);
        this.bean = bean;
    }

    @Override
    public int getCount() {
        return bean.getInventoryList().size();
    }

    @Override
    public Object getItem(int position) {
        return bean.getInventoryList().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        InventoryMgrBean.DataBean.InventoryListBean item = (InventoryMgrBean.DataBean.InventoryListBean)getItem(position);
        ViewHolder viewHolder;
        if (convertView == null || (convertView != null && convertView.getTag() == null)) {
            convertView = inflater.inflate(R.layout.view_inventory_list, null);
            viewHolder = new ViewHolder();
            //初始化控件
            initView(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //初始化数据
        initData(mContext, viewHolder, convertView, item);
        return convertView;
    }


    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.inventoryAddressCode = (TextView) view.findViewById(R.id.inventoryAddressCode);
        viewHolder.inventoryState = (TextView) view.findViewById(R.id.inventoryState);
        viewHolder.storageTyp = (TextView) view.findViewById(R.id.storageTyp);
        viewHolder.inventoryVoucher = (TextView) view.findViewById(R.id.inventoryVoucher);
        viewHolder.positionState = (TextView) view.findViewById(R.id.positionState);
        viewHolder.planDate = (TextView) view.findViewById(R.id.planDate);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, InventoryMgrBean.DataBean.InventoryListBean  item) {

        viewHolder.inventoryAddressCode.setText(item.getInventoryAddressCode()+"-"+item.getInventoryAddressValue());
        viewHolder.inventoryState.setText(item.getInventoryState());
        viewHolder.storageTyp.setText(item.getStorageType());
        viewHolder.storageTyp.setVisibility(View.GONE);
        viewHolder.inventoryVoucher.setText(item.getInventoryVoucher());
        viewHolder.positionState.setText(item.getPositionState());
        viewHolder.planDate.setText(item.getPlanDateBegin()+"到"+item.getPlanDateEnd());
        //事件处理
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, InventoryOverViewActivity.class);
                intent.putExtra(InventoryExtra.INVENTORY_VOUCHER,item.getInventoryVoucher());
                intent.putExtra(InventoryExtra.VOUCHER_TYPE,item.getVoucherType());
                intent.putExtra(InventoryExtra.FACTORY_CODE,item.getFactoryCode());
                intent.putExtra(InventoryExtra.INVENTORY_ADDRESS_CODE,item.getInventoryAddressCode());
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView
                inventoryAddressCode,
                inventoryState,
                storageTyp,
                inventoryVoucher,
                positionState,
                planDate;
    }

}
